module common.gui;

import std.math, std.stdio;

import common.gshare, common.wil;
import share.dlist;

import allegro5.allegro;
import allegro5.allegro_image;


const WINLEFT			= 0;
const WINTOP			= 60;
const WINRIGHT			= 800; //800-60;
const BOTTOMEDGE		= 600 - 30; //À©µµÀÇ BottomÀÌ WINBOTTOMÀ» ³ÑÀ» ¼ö ¾øÀ½.

const _CLIP_WIDTH		= 800;


/+public class GameWnd
{ 
	public int			m_nID;					// À©µµ¿ì °ü¸®ÀÚ°¡ ÀÌÀ©µµ¿ì¸¦ ±¸ºÐÇÒ ½Äº°ÀÚ.
	public Rectangle	m_rcImgWnd;				// À©µµ¿ìÀÇ ÀÌ¹ÌÁöÀÇ¿µ¿ª. 
	public Rectangle	m_rcWnd;				// À©µµ¿ìÀÇ È­¸é¿µ¿ª. 

	public int			m_nFrameImgIdx;			// ÀÌ¹ÌÁö °ü·Ã º¯¼öµé. 
	public ImageArray	m_pxWndImage;

	public bool			m_bActive;				// À©µµ¿ì È°¼ºÈ­ ¿©ºÎ. 
	public bool			m_bFocused; 
	public bool			m_bMoving;				// À©µµ¿ì ¿òÁ÷ÀÌ´ÂÁöÀÇ ¿©ºÎ.
	public bool			m_bCanMove;				// À©µµ¿ì °íÁ¤¿©ºÎ.

	public int			m_nWidth;
	public int			m_nHeight;

	public Point		m_bGrabbedMousePos;
	// 3DÅ×½ºÆ®
	public ALLEGRO_BITMAP*	m_avBoard;

	public this()
	{
		m_nID			= 0; 
		m_nFrameImgIdx	= 0;
		m_nWidth		= 0;
		m_nHeight		= 0;
		m_pxWndImage	= null;
		m_bActive		= false;
		m_bFocused		= false;
		m_bMoving		= false;
		m_bCanMove		= false;
		m_rcWnd.setRect(0, 0, 0, 0);
		m_rcImgWnd.setRect(0, 0, 0, 0);
		m_bGrabbedMousePos = Point.init;
		m_avBoard		= null;
	}
	
	public void RenderWindow()
	{
		if (m_bActive)
		{
			auto bmp = m_pxWndImage.GetCachedSurface(m_nFrameImgIdx);
			if (bmp !is null)
			{
				al_draw_bitmap_region(m_avBoard, 0, 0, _CLIP_WIDTH, 600, m_rcWnd.Left, m_rcWnd.Top, 0);
			}
		}
	}

	public void ResetWndImg(int nIdx)
	{
		auto bmp = m_pxWndImage.GetCachedSurface(nIdx);
		if (bmp !is null)
		{
			m_nFrameImgIdx = nIdx;
			m_rcImgWnd.setRect(m_rcWnd.Left, m_rcWnd.Top, 
					m_rcWnd.Left + al_get_bitmap_width(bmp),
					m_rcWnd.Top + al_get_bitmap_height(bmp));

			m_nWidth = m_rcImgWnd.Right - m_rcWnd.Left;
			m_nHeight = m_rcImgWnd.Bottom - m_rcWnd.Top;

			m_rcWnd = m_rcImgWnd;
		}
	}
	
	public void CreateGameWnd(int nID, ImageArray pxWndImage, int nFrameImgIdx, bool bCanMove, int nStartX, int nStartY, int nWidth = 0, int nHeight = 0)
	{
		m_nID			= nID;
		m_nFrameImgIdx	= nFrameImgIdx;
		m_pxWndImage	= pxWndImage;
		m_bCanMove		= bCanMove;

		auto bmp = m_pxWndImage.GetCachedSurface(m_nFrameImgIdx);
		if (bmp !is null)
		{
			m_rcImgWnd.setRect(nStartX, nStartY, 
					nStartX + al_get_bitmap_width(bmp),
					nStartY + al_get_bitmap_height(bmp));
		}

		m_nWidth = ( ! nWidth ) ? m_rcImgWnd.Right - m_rcImgWnd.Left: nWidth;
		m_nHeight = ( ! nHeight ) ? m_rcImgWnd.Bottom - m_rcImgWnd.Top : nHeight;

		m_rcWnd.setRect(nStartX, nStartY, nStartX + m_nWidth, nStartY + m_nHeight);
	}
	
	public void SetReadyMove(Point ptMouse)
	{
		if ( m_bActive && m_bFocused && m_bCanMove )
		{
			m_bGrabbedMousePos.X = ptMouse.X - m_rcWnd.Left;
			m_bGrabbedMousePos.Y = ptMouse.Y - m_rcWnd.Top;
		}
	}
	
	public void MoveWindow(Point ptMouse)
	{
		if ( m_bActive && m_bFocused && m_bCanMove )
		{
			m_rcWnd.setRect(ptMouse.X - m_bGrabbedMousePos.X, ptMouse.Y - m_bGrabbedMousePos.Y, 
					ptMouse.X - m_bGrabbedMousePos.X + m_nWidth, ptMouse.Y - m_bGrabbedMousePos.Y + m_nHeight); 
		}
	}
	
	protected bool IsInRect(int nPosX, int nPosY)	// Á¤È®ÇÏ°Ô Rect ¾ÈÇØ ÀÖ´Â°¡¸¦ Ã¼Å©
	{
		if ((nPosX >= m_rcWnd.Left) && (nPosX < m_rcWnd.Left + m_rcWnd.Right) && (nPosY >= m_rcWnd.Top) && (nPosY < m_rcWnd.Top + m_rcWnd.Bottom))
			return true;
		return false;
	}
		
	/*__inline RECT GetGameWndRect()
	{
		return m_rcWnd;
	}*/
	@property bool isActive() const nothrow
    {
		return m_bActive;
    }

	@property void isActive(bool value)
    {
		m_bActive = value;
    }

	@property bool isFocused() const nothrow
    {
		return m_bFocused;
    }

	@property void isFocused(bool value)
    {
		m_bFocused = value;
    }
}+/


public class GameWnd
{
	public int			m_nID;					// À©µµ¿ì °ü¸®ÀÚ°¡ ÀÌÀ©µµ¿ì¸¦ ±¸ºÐÇÒ ½Äº°ÀÚ.

	public int			m_nFrameImgIdx;		// ÀÌ¹ÌÁö °ü·Ã º¯¼öµé. 
	public ImageArray 	m_pxWndImage;

	public bool			m_bActive;				// À©µµ¿ì È°¼ºÈ­ ¿©ºÎ. 
	public bool			m_bFocused; 
	public bool			m_bMoving;				// À©µµ¿ì ¿òÁ÷ÀÌ´ÂÁöÀÇ ¿©ºÎ.
	public bool			m_bCanMove;				// À©µµ¿ì °íÁ¤¿©ºÎ.

	public int			m_nX;
	public int			m_nY;
	public int			m_nWidth;
	public int			m_nHeight;

	public Point		m_bGrabbedMousePos;
	// 3DÅ×½ºÆ®
	public ALLEGRO_BITMAP* m_avBoard;


	public this()
	{
		m_nID			= 0; 
		m_nFrameImgIdx	= 0;
		m_nX			= 0;
		m_nY			= 0;
		m_nWidth		= 0;
		m_nHeight		= 0;
		m_pxWndImage	= null;
		m_bActive		= false;
		m_bFocused		= false;
		m_bMoving		= false;
		m_bCanMove		= true;
		m_bGrabbedMousePos = Point.init;
		m_avBoard		= null;
	}
	
	public void CreateGameWnd(int nID, ImageArray pxWndImage, int nIdx, bool bCanMove, int nStartX, int nStartY, int nWidth = 0, int nHeight = 0)
	{
		m_nID			= nID;
		m_nFrameImgIdx	= nIdx;
		m_pxWndImage	= pxWndImage;
		m_bCanMove		= bCanMove;

		resetWindow(nIdx, nWidth, nHeight, nStartX, nStartY);
	}

	public bool resetWindow(int nIdx, int nWidth = 0, int nHeight = 0, int nStartX = 0, int nStartY = 0)
	{
		auto bmp = m_pxWndImage.GetCachedSurface(nIdx);
		if ( bmp !is null )
		{
			m_nFrameImgIdx = nIdx;
			m_avBoard = bmp;

			m_nX = nStartX;
			m_nY = nStartY;
			m_nWidth = nWidth ? nWidth : al_get_bitmap_width(bmp);
			m_nHeight = nHeight ? nHeight : al_get_bitmap_height(bmp);

			return true;
		}

		return false;
	}
	
	public void RenderWindow()
	{
		if (m_bActive)
		{
			al_draw_bitmap_region(m_avBoard, 0, 0, _CLIP_WIDTH, 600, m_nX, m_nY, 0);
		}
	}

	public void OnMoveWindow(Point ptMouse)
	{
		if (m_bActive && m_bFocused && m_bCanMove)
		{
			m_nX = ptMouse.X - m_bGrabbedMousePos.X;
			m_nY = ptMouse.Y - m_bGrabbedMousePos.Y;

			//m_nWidth = ptMouse.X - m_bGrabbedMousePos.X + m_nWidth;
			//m_nHeight = ptMouse.Y - m_bGrabbedMousePos.Y + m_nHeight;
		}
	}

	// À©µµ¿ì°¡ ¿òÁ÷ÀÌ±â ½ÃÀÛÇÒ¶§ ¸¶¿ì½º¿ÍÀÇ GapÀ» ¾ò´Â´Ù.
	public void SetReadyMove(Point ptMouse)
	{
		if (m_bActive && m_bFocused && m_bCanMove)
		{
			m_bGrabbedMousePos.setPoint(ptMouse.X - m_nX, ptMouse.Y - m_nY);
		}
	}

	public bool OnButtonDown(Point ptMouse)		// Button Down
	{
		if (IsInRect(ptMouse.X, ptMouse.Y))
		{	
			m_bFocused = true;
			SetReadyMove(ptMouse);
		}
		
		return false;
	}

	public bool OnButtonUp(Point ptMouse)		// Button Up
	{
		m_bFocused = false;
		
		return false;
	}

	protected bool IsInRect(int nPosX, int nPosY)	// Á¤È®ÇÏ°Ô Rect ¾ÈÇØ ÀÖ´Â°¡¸¦ Ã¼Å©
	{
		if ((nPosX >= m_nX) && (nPosX < m_nX + m_nWidth) && (nPosY >= m_nY) && (nPosY < m_nY + m_nHeight))
			return true;
		return false;
	}

	/*__inline RECT GetGameWndRect()
	{
		return m_rcWnd;
	}*/
	@property bool isActive() const nothrow
    {
		return m_bActive;
    }

	@property void isActive(bool value)
    {
		m_bActive = value;
    }

	@property bool isFocused() const nothrow
    {
		return m_bFocused;
    }

	@property void isFocused(bool value)
    {
		m_bFocused = value;
    }
}

const _BTN_STATE_NORMAL			= 0;
const _BTN_STATE_FOCUS			= 1;
const _BTN_STATE_CLICK			= 2;

const _BTN_TYPE_NOFOCUS			= 0;
const _BTN_TYPE_FOCUS			= 1;
	
	
class GameBtn
{ 
	public Rectangle		m_rcBtn;
	
	public bool				m_bActive;

	public ImageArray		m_pxBtnImage;
	public int				m_nFocusImgIdx;			// ÀÌ¹ÌÁö °ü·Ã º¯¼öµé. 
	public int				m_nClickImgIdx;			

	public ubyte			m_bBtnType;				// 0 : Æ÷Ä¿½º »óÅÂ¸¦ °¡ÁöÁö ¾Ê´Â´Ù. 1: Æ÷Ä¿½º ÀÖÀ½.
	public ubyte			m_bBtnState;			// 0 : ±×´ë·Î... 1: ¸¶¿ì½º Æ÷Ä¿½º»óÅÂ. 2: ¸¶¿ì½º Å¬¸¯»óÅÂ.
	
	private int				m_nCurrImgIdx;

	
	public this()
	{
		m_bBtnType		= _BTN_TYPE_FOCUS;
		m_bBtnState		= _BTN_STATE_FOCUS;
		m_bActive		= false;
		m_pxBtnImage	= null;
		m_nFocusImgIdx	= -1;
		m_nClickImgIdx	= -1;
		m_nCurrImgIdx	= -1;
		m_rcBtn.setRect(0, 0, 0, 0);
	}

	public void setPosition(int nStartX, int nStartY)
	{
		auto bmp = m_pxBtnImage.GetCachedSurface(m_nCurrImgIdx);
		if ( bmp !is null )
		{
			m_rcBtn.setRect(nStartX, nStartY, 
				al_get_bitmap_width(bmp),
				al_get_bitmap_height(bmp));
		}
	}
	
	public Point getPosition()
	{
		return Point(m_rcBtn.Left, m_rcBtn.Top);
	}
	
	public void CreateGameBtn(ImageArray pxBtnImage, int nFocusImgIdx, int nClickImgIdx, int nStartX, int nStartY, ubyte bBtnType = _BTN_TYPE_FOCUS)
	{
		m_bBtnType		= bBtnType;
		m_bBtnState		= _BTN_STATE_NORMAL;
		m_pxBtnImage	= pxBtnImage;
		m_nFocusImgIdx	= nFocusImgIdx;
		m_nClickImgIdx	= nClickImgIdx;
		m_nCurrImgIdx	= nFocusImgIdx;
		
		setPosition(nStartX, nStartY);
	}
	
	public void ShowGameBtn()
	{
		if (m_bActive)
		{
			/*if ( m_bBtnState == _BTN_STATE_FOCUS && m_bBtnType == _BTN_TYPE_FOCUS )
			{
				auto bmp = m_pxBtnImage.GetCachedSurface(m_nFocusImgIdx);
				if ( bmp !is null )
				{
					al_draw_bitmap_region(bmp, 0, 0, _CLIP_WIDTH, 600, m_rcBtn.Left, m_rcBtn.Top, 0);
				}
			}
			else if ( m_bBtnState == _BTN_STATE_CLICK )
			{
				auto bmp = m_pxBtnImage.GetCachedSurface(m_nClickImgIdx);
				if ( bmp !is null )
				{
					al_draw_bitmap_region(bmp, 0, 0, _CLIP_WIDTH, 600, m_rcBtn.Left, m_rcBtn.Top, 0);
				}
			}*/
			
			if ( m_bBtnState != _BTN_STATE_CLICK)
			{
				auto bmp = m_pxBtnImage.GetCachedSurface(m_nFocusImgIdx);
				if ( bmp !is null )
				{
					al_draw_bitmap_region(bmp, 0, 0, _CLIP_WIDTH, 600, m_rcBtn.Left, m_rcBtn.Top, 0);
				}
			}
			else
			{
				auto bmp = m_pxBtnImage.GetCachedSurface(m_nClickImgIdx);
				if ( bmp !is null )
				{
					al_draw_bitmap_region(bmp, 0, 0, _CLIP_WIDTH, 600, m_rcBtn.Left, m_rcBtn.Top, 0);
				}
			}
		}
	}

	public void ResetGameBtn(int nFocusImgIdx, int nClickImgIdx)
	{
		m_bBtnState		= _BTN_STATE_NORMAL;
		m_nFocusImgIdx	= nFocusImgIdx;
		m_nClickImgIdx	= nClickImgIdx;
		m_nCurrImgIdx	= nFocusImgIdx;
		
		int nStartX = m_rcBtn.Left;
		int nStartY = m_rcBtn.Top;
		setPosition(nStartX, nStartY);
	}
	
	public void OnMouseMove(Point ptMouse)
	{
		if ( IsInRect(ptMouse.X, ptMouse.Y) && (m_bBtnState != _BTN_STATE_CLICK) )
		{
			m_bBtnState = _BTN_STATE_FOCUS;
			return;
		}
		else if ( m_bBtnState == _BTN_STATE_CLICK )
		{
			return;
		}
		m_bBtnState = _BTN_STATE_NORMAL;
	}
	
	public bool OnLButtonDown(Point ptMouse)
	{
		if ( IsInRect(ptMouse.X, ptMouse.Y) )
		{
			m_bBtnState = _BTN_STATE_CLICK;
			return true;
		}
		return false;
	}
	
	public bool OnLButtonUp(Point ptMouse)
	{
		m_bBtnState = _BTN_STATE_NORMAL;
		if ( IsInRect(ptMouse.X, ptMouse.Y) )
		{
			return true;
		}
		return false;
	}
	
	protected bool IsInRect(int x, int y)	// Á¤È®ÇÏ°Ô Rect ¾ÈÇØ ÀÖ´Â°¡¸¦ Ã¼Å©
	{
		if ((x >= m_rcBtn.Left) && (x < m_rcBtn.Left + m_rcBtn.Right) && (y >= m_rcBtn.Top) && (y < m_rcBtn.Top + m_rcBtn.Bottom))
			return true;
		return false;
	}

	public Rectangle getBtnRect()
	{
		return m_rcBtn;
	}
	
	@property bool active() const nothrow
    {
		return m_bActive;
    }

	@property void active(bool value)
    {
		m_bActive = value;
    }
	
	@property ubyte state() const nothrow
    {
		return m_bBtnState;
    }

	@property void state(ubyte value)
    {
		m_bBtnState = value;
		if ( m_bBtnType == _BTN_TYPE_NOFOCUS && m_bBtnState == _BTN_STATE_FOCUS )
		{
			m_bBtnState = _BTN_STATE_NORMAL;
		}
    }
}




final class WinManager
{	
	private DList!GameWnd m_xWndOrderList;
	private bool m_bMoveFocusedWnd;
	
	public this()
	{
	}

	private GameWnd GetWindow(int nID)
	{
		if ( m_xWndOrderList.GetCounter() != 0 )
		{
			m_xWndOrderList.MoveCurrentToTop();

			GameWnd currWnd;
			for ( int nCnt = 0; nCnt < m_xWndOrderList.GetCounter(); nCnt++ )
			{
				currWnd = m_xWndOrderList.GetCurrentData();

				if ( currWnd.m_nID == nID )
					return currWnd;
				
				m_xWndOrderList.MoveNextNode();
			}
		}
		
		return null;
	}

	public GameWnd GetWindowInMousePos(Point ptMouse)
	{
		if ( m_xWndOrderList.GetCounter() != 0 )
		{
			GameWnd currWnd;
			m_xWndOrderList.MoveCurrentToTail();

			for ( int nCnt = m_xWndOrderList.GetCounter()-1; nCnt >= 0 ; nCnt-- )
			{
				currWnd = m_xWndOrderList.GetCurrentData();

				if (currWnd.IsInRect(ptMouse.X, ptMouse.Y))
					return currWnd;

				m_xWndOrderList.MovePreviousNode();
			}
		}

		return null;
	}

	public void AddWindowToList(GameWnd window)
	{
		m_xWndOrderList.AddNode(window);
	}

	public void DeleteWindowToList(GameWnd window)
	{
		if ( m_xWndOrderList.GetCounter() != 0 )
		{
			m_xWndOrderList.MoveCurrentToTop();

			GameWnd currWnd;
			for ( int nCnt = 0; nCnt < m_xWndOrderList.GetCounter(); nCnt++ )
			{
				currWnd = m_xWndOrderList.GetCurrentData();

				if ( currWnd == window )
				{
					m_xWndOrderList.DeleteCurrentNode();
					break;
				}

				m_xWndOrderList.MoveNextNode();
			}
		}
	}

	public bool WindowActivate(GameWnd window)
	{
		// »õ·Î¿î À©µµ¿ì¿¡ Æ÷Ä¿½º¸¦ ÁÖ±âÀ§ÇØ¼­ ¸ðµç À©µµ¿ìÀÇ Æ÷Ä¿½º¸¦ FALSE½ÃÄÑÁØ´Ù.
		bool bRet = false;

		AllWindowsFocusOff();

		if (window !is null)
		{
			if ( window.isActive )
			{
				DeleteWindowToList(window);
				window.isActive = false;
				bRet = false;
			}
			else
			{
				AddWindowToList(window);
				window.isActive = true;
				bRet = true;
			}
		}

		return bRet;
	}

	public void WindowFocusChangedAndSetReadyMove(GameWnd window, Point ptMouse)
	{
		if ( m_xWndOrderList.GetCounter() != 0 )
		{
			AllWindowsFocusOff();

			DeleteWindowToList(window);
			AddWindowToList(window);
			// ¸¶¿ì½º Å¬¸¯ÀÌ µÈ»óÅÂÀÌ¸é ÀÌµ¿½ÃÅ³ ÁØºñ°¡ µÈ»óÅÂÀÌ´Ù.
			m_bMoveFocusedWnd = true;

			if (window !is null)
			{
				window.isFocused = true;
				window.SetReadyMove(ptMouse);
			}
		}
	}

	public void MoveTopWindow(Point ptMouse)
	{
		if ( m_xWndOrderList.GetCounter() != 0 )
		{
			GameWnd currWnd;
			m_xWndOrderList.MoveCurrentToTail();
			currWnd = m_xWndOrderList.GetCurrentData();
			
			if ( m_bMoveFocusedWnd )
				currWnd.OnMoveWindow(ptMouse);
		}
	}

	public void AllWindowsFocusOff()
	{
		if ( m_xWndOrderList.GetCounter() != 0 )
		{
			m_xWndOrderList.MoveCurrentToTop();

			GameWnd currWnd;
			for ( int nCnt = 0; nCnt < m_xWndOrderList.GetCounter(); nCnt++ )
			{
				currWnd = m_xWndOrderList.GetCurrentData();
				currWnd.isFocused = false;
				
				m_xWndOrderList.MoveNextNode();
			}
		}
	}

	public bool IsTopWindow(GameWnd window)
	{
		if ( m_xWndOrderList.GetCounter() != 0 )
		{
			GameWnd currWnd;
			m_xWndOrderList.MoveCurrentToTail();
			currWnd = m_xWndOrderList.GetCurrentData();

			if ( window == currWnd )
				return true;
		}
		
		return false;
	}
	
	private void ShowWndList()
	{
		if ( m_xWndOrderList.GetCounter() != 0 )
		{
			m_xWndOrderList.MoveCurrentToTop();

			GameWnd currWnd;

			for ( int nCnt = 0; nCnt < m_xWndOrderList.GetCounter(); nCnt++ )
			{
				currWnd = m_xWndOrderList.GetCurrentData();
				
				currWnd.RenderWindow();

				m_xWndOrderList.MoveNextNode();
			}
		}
	}
	
	public void RenderInterface(int nLoopTime, Point ptMousePos)
	{
		int nX, nY, nCnt;

		// À©µµ¿ì.
		ShowWndList();
	}
	
	
	
	public bool OnLButtonDown(Point ptMouse)
	{
		/*if(m_xMsgBox.IsActive())	// ¿ë±â
		{
			m_xMsgBox.OnButtonDown(ptMouse);
			return TRUE;
		}
		else
		{
			if ( m_xScrlBar.OnLButtonDown(ptMouse) )
			{
				FLOAT	fScrlRate;

				fScrlRate = m_xScrlBar.GetScrlRate();
				m_nFstLine  = (m_xChat.GetCounter()-1)*fScrlRate;

				return TRUE;
			}

			for( INT nCnt = 0; nCnt < _MAX_INTER_BTN; nCnt++ )
			{
				if ( m_xInterBtn[nCnt].OnLButtonDown(ptMouse) )
					return TRUE;
			}*/

			GameWnd currWnd;
			currWnd = GetWindowInMousePos(ptMouse);

			if ( currWnd !is null )
			{
				if ( !currWnd.OnButtonDown(ptMouse) )
					WindowFocusChangedAndSetReadyMove(currWnd, ptMouse);
					
				return true;
			}
			else
				AllWindowsFocusOff();
		//}
	
		return false;
	}

	public bool OnLButtonUp(Point ptMouse)
	{
		m_bMoveFocusedWnd = false;

		/*if ( m_xMsgBox.IsActive() )	// ¿ë±â
		{
			HRESULT hResult;
			hResult = m_xMsgBox.OnButtonUp(ptMouse);
			if ( hResult != 0 )
			{
				switch ( m_nIdxMsgBox )
				{
				case 12:
					{
						switch ( hResult )	// ¿¡µðÆ® ¹Ú½º°¡ ¾øÀ½.
						{
						case 2:
							{
							   DWORD dwCurrTick = timeGetTime();
							   if ( (dwCurrTick - g_xGameProc.m_xMyHero.m_dwLastPKStruckTime> 10000) &&
									(dwCurrTick - g_xGameProc.m_xMyHero.m_dwLastMagicTime   > 10000) &&
									(dwCurrTick - g_xGameProc.m_xMyHero.m_dwLastHitTime		> 10000) || 
									(g_xGameProc.m_xMyHero.m_bCurrMtn					== _MT_DIE ) )
									SendMessage(g_xMainWnd.GetSafehWnd(), WM_DESTROY, NULL, NULL);
							   else
							   {
								   DWORD dwFont, dwBack;

								   dwFont = GetChatColor(3);
								   dwBack = GetChatColor(2);

								   MsgAdd(dwFont, dwBack, "ÀüÅõÁß¿¡´Â Á¢¼ÓÀ» ²÷À» ¼ö ¾ø½À´Ï´Ù.");
							   }
							}
							break;
						case 4:
							m_xMsgBox.HideMessageBox();
							break;
						}
					}
					break;
				}
			}
		}
		else
		{
			if ( m_xScrlBar.OnLButtonUp(ptMouse) )
			{
				return TRUE;
			}

			for( INT nCnt = 0; nCnt < _MAX_INTER_BTN; nCnt++ )
			{
				if ( m_xInterBtn[nCnt].OnLButtonUp(ptMouse) )
				{
					switch ( nCnt )
					{
					case _BTN_ID_QUEST:		WindowActivate(_WND_ID_QUEST);					break;
					case _BTN_ID_OPTION:	WindowActivate(_WND_ID_OPTION);					break;
					case _BTN_ID_HORSE:		WindowActivate(_WND_ID_HORSE);					break;

					case _BTN_ID_CHANGE:	WindowActivate(_WND_ID_EXCHANGE);				break;
					case _BTN_ID_GUILD:		
						g_xClientSocket.SendGuildHome();
						break;
					case _BTN_ID_GROUP:		WindowActivate(_WND_ID_GROUP);					break;
					case _BTN_ID_POPUP:		WindowActivate(_WND_ID_CHATPOP);				break;
					case _BTN_ID_BAG:		WindowActivate(_WND_ID_INVENTORY);				break;
					case _BTN_ID_MUGONG:	m_bShowKey = !m_bShowKey;						break;
					case _BTN_ID_BELT:
						{
							if ( m_shBeltHeight >= 46 )	
							{
								m_shBeltHeight = 46;
							}
							else if ( m_shBeltHeight <= 0 )		
							{
								m_shBeltHeight = 0;
							}

							if ( m_shBeltHeight == 46 )
							{
								m_bBeltState = _BELT_STATE_DOWN;
							}
							else if ( m_shBeltHeight == 0 )
							{
								m_bBeltState = _BELT_STATE_UP;
							}
						}
						break;
					case _BTN_ID_EXIT:
						{
							CHAR	  szMsg[MAX_PATH];
							HINSTANCE hLib;
							m_nIdxMsgBox = 12;
							hLib = LoadLibrary("Ats.dll");						// Load Dll
							LoadString(hLib,m_nIdxMsgBox,szMsg, MAX_PATH);	
							FreeLibrary(hLib);
							m_xMsgBox.ShowMessageBox(szMsg, 2);					// YES/No ?
						}
						break;
					case _BTN_ID_MAGIC:
						if ( m_xStatusWnd.GetStatusWndType() != _TYPE_SETMAGIC )
						{
							if ( !WindowActivate(_WND_ID_STATUS) )		WindowActivate(_WND_ID_STATUS);
						}
						else
							WindowActivate(_WND_ID_STATUS);
						m_xStatusWnd.SetStatusWndType(_TYPE_SETMAGIC);
						break;
					case _BTN_ID_CHAR:
						if ( m_xStatusWnd.GetStatusWndType() != _TYPE_EQUIP )
						{
							if ( !WindowActivate(_WND_ID_STATUS) )		WindowActivate(_WND_ID_STATUS);
						}
						else
							WindowActivate(_WND_ID_STATUS);
						m_xStatusWnd.SetStatusWndType(_TYPE_EQUIP);
						break;
					}
				}
			}*/

			GameWnd currWnd;
			currWnd = GetWindowInMousePos(ptMouse);

			// °¢ À©µµ¿ìÀÇ OnLButtonUpÀÇ ¸®ÅÏ°ªÀÌ TRUEÀÌ¸é À©µµ¿ìÁî´Â CloseµÈ´Ù.
			if ( currWnd !is null )
			{
				if ( currWnd.OnButtonUp(ptMouse) )
					WindowActivate(currWnd);

				return true;
			}
		//}
	
		return false;
	}

	public bool OnMouseMove(Point ptMouse)
	{
		MoveTopWindow(ptMouse);

		/*if ( m_xScrlBar.OnMouseMove(ptMouse) )
		{
			FLOAT	fScrlRate;

			fScrlRate = m_xScrlBar.GetScrlRate();
			m_nFstLine  = (m_xChat.GetCounter()-1)*fScrlRate;
			return TRUE;
		}

		for( INT nCnt = 0; nCnt < _MAX_INTER_BTN; nCnt++ )
			m_xInterBtn[nCnt].OnMouseMove(ptMouse);*/

		GameWnd currWnd;
		currWnd = GetWindowInMousePos(ptMouse);

		if ( currWnd !is null )
		{
			//currWnd.OnMouseMove(ptMouse);
			currWnd.OnMoveWindow(ptMouse);

			return true;
		}
		else
		{
			/*m_xStatusWnd.SetStatusBtnInit();
			m_xInventoryWnd.SetInvenBtnInit();
			m_xChatPopWnd.SetStatusBtnInit();
			m_xGuildMasterWnd.SetStatusBtnInit();
			m_xOptionWnd.SetStatusBtnInit();
			m_xQuestWnd.SetStatusBtnInit();
			m_xHorseWnd.SetStatusBtnInit();
			m_xExchangeWnd.SetStatusBtnInit();*/
		}

		return false;
	}

}